package feedback;

import java.util.ArrayList;
import java.util.List;

import util.Entity;
import books.BookInfo;

/**
 * A book (BookInfo) and a feedback given by the users.
 */
public class BookInfoFeedback extends Entity {

    private static final long serialVersionUID = 1L;

    private BookInfo book;

    private List<PersonFeedback> peopleFeedback;

    public BookInfoFeedback() {
        super();
        // empty constructor
        this.peopleFeedback = new ArrayList<PersonFeedback>();
    }

    public BookInfoFeedback(final BookInfo aBinfo) {
        super();
        this.book = aBinfo;
        this.peopleFeedback = new ArrayList<PersonFeedback>();
    }

    public BookInfo getBook() {
        return this.book;
    }

    public void setBook(final BookInfo book) {
        this.book = book;
    }

    public List<PersonFeedback> getPeopleFeedback() {
        return this.peopleFeedback;
    }

    public void setPeopleFeedback(final List<PersonFeedback> usersRating) {
        this.peopleFeedback = usersRating;
    }

    public double getRating() {
        /**
         * Returns the average rating (double).
         */
        double tmpScore = 0;
        if (!this.getPeopleFeedback().isEmpty()) {
            for (PersonFeedback uf : this.getPeopleFeedback()) {
                tmpScore += uf.getRateValue();
            }
            tmpScore = tmpScore / this.getPeopleFeedback().size();
        }
        return tmpScore;

    }

    public void addUserFeedback(final PersonFeedback feedback) {
        this.getPeopleFeedback().add(feedback);
    }

}
